﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsMine.DataManagers;
using WebApp1.Models;

namespace WebApp1.AdminSection
{
    public partial class KeyWordEditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SecurityCheckManager.IsAdminPortalEabled() == false)
            {
                Response.StatusCode = 403;
                Response.Write("You should be the admin....");
                return;
            }

            BindCategoriesToDropDownListBox();
        }

        private void BindCategoriesToDropDownListBox(bool refresh=false)
        {
            //cmbCategoryName.DataTextField = ".";
            if (refresh==true || cmbCategoryName.Items == null || cmbCategoryName.Items.Count == 0)
            {
                List<string> keyWordCategories = NewsMine.DataManagers.KeyWordManager.GetAllCategories();

                cmbCategoryName.DataSource = keyWordCategories;
                cmbCategoryName.DataBind();

                cmbCategoryName.SelectedIndex = -1;
            }
        }

        protected void btnGetKeyWords_Click(object sender, EventArgs e)
        {
            if (cmbCategoryName.SelectedIndex > -1)
            {
                string selectedCategory = cmbCategoryName.SelectedValue.ToString();

                SelectCategory(selectedCategory);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbCategoryName.SelectedIndex > -1)
            {
                string selectedCategory = cmbCategoryName.SelectedValue.ToString();

                if (!string.IsNullOrEmpty(txtKeywords.Text))
                {

                    var editedKeywords = txtKeywords.Text.Split('\n').Where(word => !string.IsNullOrEmpty(word)).ToList<string>();

                    NewsMine.DataManagers.KeyWordManager.OverrideCompleteKeyWordsToCategory(selectedCategory, editedKeywords);

                    SelectCategory(selectedCategory);

                    lblStatusAddMessage.Text = "Added successfully.";
                }

            }
        }

        protected void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbCategoryName.SelectedIndex > -1)
            {
                string selectedCategory = cmbCategoryName.SelectedValue.ToString();

                if (!string.IsNullOrEmpty(txtKeywords.Text))
                {

                    var editedKeywords = txtAddedKeywords.Text.Split('\n').Where(word => !string.IsNullOrEmpty(word)).ToList<string>();

                    NewsMine.DataManagers.KeyWordManager.AddKeyWordsToCategory(selectedCategory, editedKeywords);

                    SelectCategory(selectedCategory);

                    lblStatusAddMessage.Text = "Saved successfully.";

                }

            }
        }

        protected void btnCreateNewCategory_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtNewCategory.Text))
            {
                lblCreateCategory.Text = "Give proper category name....";
                return;
            }

            List<string> keyWordCategories = NewsMine.DataManagers.KeyWordManager.GetAllCategories();

            if (keyWordCategories.Contains(txtNewCategory.Text.ToLower()))
            {
                lblCreateCategory.Text = "This category is already existed";
                return;
            }

            string newCategory = txtNewCategory.Text.Replace(" ", string.Empty).Trim();

            KeyWordManager.AddKeyWordsToCategory(newCategory, newCategory);

            BindCategoriesToDropDownListBox(true);

            SelectCategory(newCategory);

            txtNewCategory.Text = string.Empty;
           

        }

        private void SelectCategory(string category)
        {
            string selectedCategory = category;

            lblSelectedCategory.Text = cmbCategoryName.SelectedValue;

            cmbCategoryName.SelectedValue = category;

            var keywords = NewsMine.DataManagers.KeyWordManager.GetAllKeywordsByCategory(selectedCategory);
            txtKeywords.Text = String.Join("\n", keywords);
         
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (cmbCategoryName.SelectedIndex > -1)
            {
                string selectedCategory = cmbCategoryName.SelectedValue.ToString();

                KeyWordManager.DeleteKeyWordCategory(selectedCategory);

                BindCategoriesToDropDownListBox(true);
                lblSelectedCategory.Text = selectedCategory + " deleted.";
            }
        }

        protected void cmbCategoryName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCategoryName.SelectedIndex > -1)
            {
                string selectedCategory = cmbCategoryName.SelectedValue.ToString();

                SelectCategory(selectedCategory);
            }
        }

    }
}